<?php
// +----------------------------------------------------------------------
// | B5Yii2CMF V3.0 [快捷通用基础管理开发平台]
// +----------------------------------------------------------------------
// | Author: 冰舞 <357145480@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace api\utils\libs;

use common\extend\exception\ErrorCode;
use common\helpers\Result;
use ReflectionException;
use yii\base\Action;
use yii\base\Controller;
use yii\base\InlineAction;
use yii\base\InvalidConfigException;
use yii\web\Application;
use yii\web\Request;
use yii\web\Response;

/**
 * Class BaseController
 * @property Request $request The request object.
 * @package api\utils\libs
 */
class BaseController extends Controller
{
    /**
     * 应用实例
     * @var Application
     */
    protected Application $app;

    /**
     * 构造函数
     */
    public function init()
    {
        parent::init();
        $this->app = \Yii::$app;
    }
	
	 public function beforeAction($action): bool
    {
        if (!parent::beforeAction($action)) return false;
        if ($this->request->isOptions) {
            $this->app->response->setStatusCode(204);
            return false;
        }
        return true;
    }

    /**
     * 错误提示
     * @param string $msg
     * @param int $code
     * @return array
     */
    public function error(string $msg = '', int $code = ErrorCode::API_ERROR): array
    {
        return Result::error($msg, $code);
    }

    /**
     * 跳转成功
     * @param string $msg
     * @param array|string $data
     * @param array $extend
     * @param int $code
     * @return array
     */
    public function success(string $msg = '', $data = [], array $extend = [], int $code = ErrorCode::API_SUCCESS): array
    {
        return Result::success($msg, $data, $extend, $code);
    }

    /**
     * 重写方法查找，支持驼峰方法
     * @param string $id
     * @return object|Action|InlineAction|null
     * @throws ReflectionException
     * @throws InvalidConfigException
     */
    public function createAction($id = '')
    {
        if ($id) {
            $actionMap = $this->actions();
            if (isset($actionMap[$id])) {
                return \Yii::createObject($actionMap[$id], [$id, $this]);
            }
            //原样的
            $methodName = 'action' . $id;
            if (method_exists($this, $methodName)) {
                $method = new \ReflectionMethod($this, $methodName);
                if ($method->isPublic() && $method->getName() === $methodName) {
                    return new InlineAction($id, $this, $methodName);
                }
            }
            //首字母大写的
            $methodName = 'action' . ucwords($id);
            if (method_exists($this, $methodName)) {
                $method = new \ReflectionMethod($this, $methodName);
                if ($method->isPublic() && $method->getName() === $methodName) {
                    return new InlineAction($id, $this, $methodName);
                }
            }
        }
        return parent::createAction($id);
    }

    /**
     * 由于设置了默认位json，所以在这个重写响应设置为html
     * @param string $view
     * @param array $params
     * @return string
     */
    public function render($view = '', $params = []): string
    {
        $this->app->response->format = Response::FORMAT_HTML;
        return parent::render($view, $params);
    }

    public function renderPartial($view, $params = []): string
    {
        $this->app->response->format = Response::FORMAT_HTML;
        return parent::renderPartial($view, $params);
    }
}
